package timer;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimerTask;

/**
 * @ClassName MyTimerTask
 * @Description From : https://www.imooc.com/video/14950
 *
 * @Author Wxg
 * @Date 2018/11/13 14:58
 * @Version 1.0
 */
public class MyTimerTaskV2 extends TimerTask {
    private String name;
    private Integer count = 0;

    public MyTimerTaskV2(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        if (count < 3) {
            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("Current exec time is : " + sdf.format(calendar.getTime()));
            System.out.println("Current exec name is : " + name);
        } else {
            cancel();
            System.out.println("Task cancel!");
        }

        count++;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
